/*
 * Copyright (c) 2010 Marc Poppleton
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.marcpoppleton.strawberry;

import org.marcpoppleton.strawberry.provider.CoCManager;
import org.marcpoppleton.strawberry.provider.Coc;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;

public class CamerasActivity extends ListActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		final Cursor c = this.managedQuery(Coc.CONTENT_URI,
				Coc.PROJECTION_IDS_AND_ALL, null, null, Coc.DEFAULT_SORT_ORDER);
		startManagingCursor(c);

		ListAdapter adapter = new SimpleCursorAdapter(this,
				android.R.layout.two_line_list_item, c, new String[] { Coc._ID,
						Coc.VALUE }, new int[] { android.R.id.text1,
						android.R.id.text2 });
		((SimpleCursorAdapter)adapter).setStringConversionColumn(c.getColumnIndex(Coc._ID));
		setListAdapter(adapter);
		getListView().setTextFilterEnabled(true);
		registerForContextMenu(getListView());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_cameras_list, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenu.ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		getMenuInflater().inflate(R.menu.menu_cameras, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		final AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
				.getMenuInfo();
		info.targetView.toString();
		Coc coc = Coc.fromCursor((Cursor) getListAdapter().getItem(
				info.position));
		switch (item.getItemId()) {
		case R.id.menu_cameras_item_delete:
			CoCManager.deleteCamera(getContentResolver(), coc.getName());
			return true;
		case R.id.menu_cameras_item_edit:
			EditCameraActivity.show(this, coc);
			return true;
		}
		return super.onContextItemSelected(item);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch(item.getItemId()){
		case R.id.menu_cameras_item_new:
			EditCameraActivity.show(this,null);
		}
		return super.onMenuItemSelected(featureId, item);
	}

	/**
	 * Starts the CamerasActivity for the specified user.
	 * 
	 * @param context
	 *            The application's environment.
	 */
	static void show(Context context) {
		final Intent intent = new Intent(context, CamerasActivity.class);
		context.startActivity(intent);
	}

}
